package com.yoga.manager.app.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.PlainVipBO;
import com.yoga.common.bo.PrivateVipBO;
import com.yoga.common.bo.VipCareBO;
import com.yoga.common.bo.YogaUserBO;
import com.yoga.common.enumcommon.SmsBusinessEnum;
import com.yoga.common.page.PageBO;
import com.yoga.common.qto.YogaUserQTO;
import com.yoga.dao.dao.PlainVipDao;
import com.yoga.dao.dao.PrivateVipDao;
import com.yoga.dao.dao.YogaUserDao;
import com.yoga.dao.domain.PlainVipDO;
import com.yoga.dao.domain.PrivateVipDO;
import com.yoga.dao.domain.YogaUserDO;
import com.yoga.manager.app.AppPlainVipManager;
import com.yoga.manager.app.AppPrivateVipManager;
import com.yoga.manager.app.AppYogaUserManager;
import com.yoga.manager.app.CaptchaCodeManager;
import com.yoga.manager.beanUtil.PlainVipBeanUtil;
import com.yoga.manager.beanUtil.PrivateVipBeanUtil;
import com.yoga.manager.beanUtil.YogaUserBeanUtil;
import com.yoga.manager.uitl.DateStyle;
import com.yoga.manager.uitl.DateUtil;
import com.yoga.manager.uitl.MD5Util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hdx on 2020/9/24.
 */
@Component
public class AppYogaUserManagerImpl implements AppYogaUserManager {
    Logger logger = LoggerFactory.getLogger(AppYogaUserManagerImpl.class);

    @Resource
    private YogaUserDao yogaUserDao;

    @Resource
    private PlainVipDao plainVipDao;

    @Resource
    private PrivateVipDao privateVipDao;

    @Resource
    private CaptchaCodeManager captchaCodeManager;

    @Resource
    private AppPlainVipManager appPlainVipManager;

    @Resource
    private AppPrivateVipManager appPrivateVipManager;

//    @Transactional(rollbackFor = Exception.class)
//    @Transactional
    @Override
    public YogaUserBO register(YogaUserBO yogaUserBO) throws AppException {
        if (yogaUserBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(yogaUserBO.getUserNo())) {
            throw new AppException(AppErrorCodeEnum.PHONE_IS_NULL);
        }
        if (StringUtils.isEmpty(yogaUserBO.getUserPasswd())) {
            throw new AppException(AppErrorCodeEnum.PASSWD_NULL);
        }
        if (StringUtils.isEmpty(yogaUserBO.getCode())) {
            throw new AppException(AppErrorCodeEnum.CODE_NULL);
        }
        if (StringUtils.isEmpty(yogaUserBO.getUserNick())) {
            throw new AppException(AppErrorCodeEnum.NICK_NULL);
        }
        YogaUserDO yogaUserDO = yogaUserDao.findByUserNo(yogaUserBO.getUserNo());
        if (yogaUserDO != null) {
            throw new AppException(AppErrorCodeEnum.ACCOUNT_IS_EXIST);
        }

        captchaCodeManager.validation(yogaUserBO.getUserNo(), yogaUserBO.getCode(), SmsBusinessEnum.PHONE_REGISTER);
        YogaUserDO insert = YogaUserBeanUtil.yogaUserBOToDO(yogaUserBO);
        insert.setUserPasswd(MD5Util.passWord(yogaUserBO.getUserPasswd()));
        int num = yogaUserDao.insert(insert);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.INSERT_DATA_NULL);
        }
        YogaUserBO result = YogaUserBeanUtil.yogaUserDOToBO(yogaUserDao.findById(insert.getId()));
        PlainVipBO plainVipBO = new PlainVipBO();
        plainVipBO.setYogaUserId(insert.getId());
        plainVipBO.setVipType(0);
        plainVipBO.setTotalPrice("0");
        plainVipBO.setVipPrice("0");
        plainVipBO.setClassNum(0);
        plainVipBO.setAboutNum(0);
        plainVipBO.setStopEndDate(DateUtil.stringToDate("2020-01-01", DateStyle.YYYY_MM_DD));
        plainVipBO.setVipStartDate(DateUtil.stringToDate("2020-01-01", DateStyle.YYYY_MM_DD));
        plainVipBO.setVipEndDate(DateUtil.stringToDate("2020-01-01", DateStyle.YYYY_MM_DD));
        int num1 = plainVipDao.insert(PlainVipBeanUtil.plainVipBOToDO(plainVipBO));

        PrivateVipDO privateVipDO = new PrivateVipDO();
        privateVipDO.setYogaUserId(insert.getId());
        privateVipDO.setVipType(0);
        privateVipDO.setTotalPrice("0");
        privateVipDO.setVipPrice("0");
        privateVipDO.setClassNum(0);
        privateVipDO.setAboutNum(0);
        int num2 = privateVipDao.insert(privateVipDO);

        return result;
    }

    @Override
    public YogaUserBO login(YogaUserBO yogaUserBO) throws AppException {
        if (yogaUserBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(yogaUserBO.getUserNo())) {
            throw new AppException(AppErrorCodeEnum.ACCOUNT_NULL);
        }
        if (StringUtils.isEmpty(yogaUserBO.getUserPasswd())) {
            throw new AppException(AppErrorCodeEnum.PASSWD_NULL);
        }
        YogaUserDO yogaUserDO = yogaUserDao.findByUserNo(yogaUserBO.getUserNo());
        if (yogaUserDO == null) {
            throw new AppException(AppErrorCodeEnum.UN_REGISTER);
        }
        if (!yogaUserDO.getUserPasswd().equals(MD5Util.passWord(yogaUserBO.getUserPasswd()))) {
            throw new AppException(AppErrorCodeEnum.PASSWD_ERROR);
        }
        YogaUserBO result = YogaUserBeanUtil.yogaUserDOToBO(yogaUserDO);
        try {
            PlainVipBO plainVipBO = appPlainVipManager.getPlainVipByUserId(yogaUserDO.getId());
            PrivateVipBO privateVipBO = appPrivateVipManager.getPrivateVipByUserId(yogaUserDO.getId());
            result.setPlainVipBO(plainVipBO);
            result.setPrivateVipBO(privateVipBO);
        } catch (Exception e) {
            logger.error("获取vip信息失败！e:{}", e);
        }

        return result;
    }

    @Override
    public Boolean updateData(YogaUserBO yogaUserBO) throws AppException {
        if (yogaUserBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (yogaUserBO.getYogaUserId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        YogaUserDO yogaUserDO = YogaUserBeanUtil.yogaUserBOToDO(yogaUserBO);
        int num = yogaUserDao.updateById(yogaUserDO);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public Boolean updatePasswd(YogaUserBO yogaUserBO) throws AppException {
        if (yogaUserBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (StringUtils.isEmpty(yogaUserBO.getUserNo())) {
            throw new AppException(AppErrorCodeEnum.PHONE_IS_NULL);
        }
        if (StringUtils.isEmpty(yogaUserBO.getCode())) {
            throw new AppException(AppErrorCodeEnum.CODE_NULL);
        }
        if (StringUtils.isEmpty(yogaUserBO.getUserPasswd())) {
            throw new AppException(AppErrorCodeEnum.PASSWD_NULL);
        }
        captchaCodeManager.validation(yogaUserBO.getUserNo(), yogaUserBO.getCode(), SmsBusinessEnum.FORGET_PASSWD);
        YogaUserDO old = yogaUserDao.findByUserNo(yogaUserBO.getUserNo());
        if (old == null) {
            throw new AppException(AppErrorCodeEnum.PHONE_NOT_REGISTER);
        }
        String pw = MD5Util.passWord(yogaUserBO.getUserPasswd());
        if (old.getUserPasswd().equals(pw)) {
            throw new AppException(AppErrorCodeEnum.PASSWD_IS_OLD_PASSWD);
        }
        YogaUserDO update = new YogaUserDO();
        update.setId(old.getId());
        update.setUserPasswd(pw);
        int num = yogaUserDao.updatePasswdById(update);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_PASSWD_ERROR);
        }

        return Boolean.TRUE;
    }

    @Override
    public YogaUserBO getUser(Long yogaUserId) throws AppException {
        if (yogaUserId == null) {
            throw new AppException(AppErrorCodeEnum.UN_LOGN);
        }
        YogaUserDO yogaUserDO = yogaUserDao.findById(yogaUserId);
        if (yogaUserDO == null) {
            return new YogaUserBO();
        }
        YogaUserBO yogaUserBO = YogaUserBeanUtil.yogaUserDOToBO(yogaUserDO);
        PlainVipBO plainVipBO = appPlainVipManager.getPlainVipByUserId(yogaUserId);
        PrivateVipBO privateVipBO = appPrivateVipManager.getPrivateVipByUserId(yogaUserDO.getId());
        yogaUserBO.setPlainVipBO(plainVipBO);
        yogaUserBO.setPrivateVipBO(privateVipBO);

        return yogaUserBO;
    }

    @Override
    public PageBO<YogaUserBO> findForPage(YogaUserQTO yogaUserQTO) throws AppException {
        if (yogaUserQTO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        PageBO<YogaUserBO> pageBO = new PageBO<>();
        pageBO.setQuery(yogaUserQTO);
        Integer count = yogaUserDao.findForPageCount(yogaUserQTO);
        if (count <= 0) {
            return pageBO;
        }
        List<YogaUserBO> resultList = YogaUserBeanUtil.doList2BOList(yogaUserDao.findForPage(yogaUserQTO));


        return null;
    }

    @Override
    public VipCareBO findVipCardByUserId(Long yogaUserId) throws AppException {
        if (yogaUserId == null) {
            throw new AppException(AppErrorCodeEnum.UN_LOGN);
        }
        VipCareBO vipCareBO = new VipCareBO(yogaUserId);

        PlainVipBO plainVipBO = appPlainVipManager.getPlainVipByUserId(yogaUserId);
        PrivateVipBO privateVipBO = appPrivateVipManager.getPrivateVipByUserId(yogaUserId);
        vipCareBO.setPlainVipBO(plainVipBO);
        vipCareBO.setPrivateVipBO(privateVipBO);

        return vipCareBO;
    }
}
